﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.BaseDoc;

public partial class Task_BaseDoc_BaseFileList : System.Web.UI.Page
{
    private string DepartmentID = string.Empty;
    private bool Flag = true;
    private int PageCount = 15;
    protected void Page_Load(object sender, EventArgs e)
    {
        DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            BindDate();
            BindType();
            Bind(PageCount, 0);
            Flag = true;
        }

    }

    private void Bind(int pageSize, int pageIndex)
    {
        int ID = int.Parse(GetID());
        var result = Doc.GetBaseDocList2().Where(o => o.SetDepartment == DepartmentID && o.TID == ID && o.CreateDate.Value.Year == int.Parse(dptYear.SelectedValue) && o.CreateDate.Value.Month == int.Parse(dptMonth.SelectedValue));
        this.UserGrid.DataSource = result.Skip(pageSize * pageIndex).Take(pageSize);
        this.ListPageContral1.ItemCount = result.Count();
        this.UserGrid.DataBind();
    }

    private void BindType()
    {
        this.dptJobType.DataSource = Doc.GetDocTypeList().Where(o => o.ParentID == 0);
        this.dptJobType.DataTextField = "TypeName";
        this.dptJobType.DataValueField = "ID";
        this.dptJobType.DataBind();
        this.dptFileType.Visible = false;
    }
    private void BindDate()
    {
        for (int i = 2008; i < 2050; i++)
        {
            dptYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i < 13; i++)
        {
            dptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        dptYear.SelectedValue = DateTime.Now.Year.ToString();
        dptMonth.SelectedValue = DateTime.Now.Month.ToString();
    }

    //获取分类ID
    private string GetID()
    {
        if (dptFileType.Visible)
            return dptFileType.SelectedValue;
        else
            return dptJobType.SelectedValue;
    }

    //绑定分类
    private void BindClass()
    {
        this.dptFileType.DataSource = Doc.GetDocTypeList().Where(o => o.ParentID == int.Parse(dptJobType.SelectedValue));
        this.dptFileType.DataTextField = "TypeName";
        this.dptFileType.DataValueField = "ID";
        this.dptFileType.DataBind();
        if (dptFileType.Items.Count == 0)
        {
            dptFileType.Visible = false;
        }
        else
        {
            dptFileType.Visible = true;
        }
    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(pageSize, PageIndex);
    }

    protected void lbtnQuery_Click(object sender, EventArgs e)
    {
        Flag = false;
        Bind(PageCount, 0);
    }

    //获取指定长度的标题
    public string GetSubString(object obj)
    {
        if (obj.ToString().Length > 30)
            return obj.ToString().Substring(0, 30) + "...";
        else
            return obj.ToString();

    }

    protected void dptJobType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindClass();
        lbtnQuery_Click(this, new EventArgs());
    }

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ListPageContral1.CurrentPageIndex) * ListPageContral1.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            BaseDocs doc = e.Row.DataItem as BaseDocs;
            HyperLink hplcaption = e.Row.FindControl("hplcaption") as HyperLink;
            hplcaption.Text = doc.Caption;
            //标题
            if (doc.TID == 1)
            {//专项工作
                hplcaption.NavigateUrl = "DocView.aspx?ID=" + doc.ID;
            }
            else if (doc.TID == 2)
            {//日常工作
                BaseDocFiles file = Doc.GetBaseDocFileByDID(UserGrid.DataKeys[e.Row.RowIndex].Value.ToString()).FirstOrDefault();
                if (file != null)
                {
                    hplcaption.NavigateUrl = "../../Document/" + doc.SetDepartment + "/" + file.FileName;
                    hplcaption.Target = "_blank";
                }
            }
            else
            {//信息反馈
                hplcaption.NavigateUrl = doc.Contents;
                hplcaption.Target = "_blank";
            }
        }
    }
    protected void ibtnDelete_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument == null)
            return;
        Doc.DeleteBaseDoc(e.CommandArgument.ToString());
        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);

    }
    protected void dptYear_SelectedIndexChanged(object sender, EventArgs e)
    {

        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);
    }
    protected void dptMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);
    }
    protected void dptFileType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);
    }
}
